package com.briup.corejava.chap12;

import com.briup.corejava.chap12.bean.Student;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

/**
 * 登陆逻辑的服务器
 * 从文件里读取学生信息放到集合中去
 * 接收客户端的学生信息进行比对
 * 比对成功：登陆成功
 * 比对失败：登陆失败
 */
public class Test065_LoginServer {
    public static void main(String[] args) throws Exception{
        ServerSocket serverSocket = null;
        Socket socket = null;
        int port = 8888;
        serverSocket = new ServerSocket(port);
        while (true) {
            socket = serverSocket.accept();
            Socket finalSocket = socket;
            Thread t = new Thread(){
                @Override
                public void run() {
                    try {
                        // 接收数据
                        ObjectInputStream ois = new ObjectInputStream(
                                finalSocket.getInputStream());
                        Object o = ois.readObject();
                        Student s = null;
                        if (o instanceof Student) {
                            s = (Student) o;
                        }
                        // 判断，从文件里读取出来的集合
                        BufferedReader br = new BufferedReader(
                                new FileReader("dir/student.txt"));
                        String line = null;
                        List<Student> students = new ArrayList<>();
                        while ((line = br.readLine()) != null) {
                            // line = 002.jack.20.888888
                            // 分割字符串
                            String[] split = line.split("[.]");
                            Student student = new Student(split[0], split[1], Integer.parseInt(split[2]), split[3]);
                            students.add(student);
                        }
                        // 定义返回的字符串
                        String resp = null;
                        // 定义一个boolean类型的标识，代表是否登陆成功
                        boolean flag = false;
                        for (Student tmp : students) {
                            if (s.getName().equals(tmp.getName())) {
                                if (s.getPassword().equals(tmp.getPassword())) {
                                    flag = true;
                                    s = tmp;
                                    break;
                                }
                            }
                        }
                        resp = flag ? "登陆成功" : "登陆失败";
                        PrintWriter pw = new PrintWriter(finalSocket.getOutputStream());
                        pw.println(resp);
                        pw.flush();

                        pw.close();
                        br.close();
                        ois.close();
                        // 登陆成功之后，在服务器端展示所有的数据信息
                        System.out.println("当前登陆的用户信息：" + s);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            };
            t.start();
        }
    }
}
